import { createWebHistory, createRouter } from 'vue-router'
import Layout from '@/layout'

// const modules = import.meta.glob('../views/**/*.vue')
// console.log(modules, 'modules')
export const constantRoutes = [
  {
    path: '/redirect/:path(.*)',
    hidden: true,
    // 定义component 刷新才会跳转到页面，否则调试只进入了redirect方法，但没有路由的跳转
    component: () => import('@/views/Redirect/index')
    // redirect: to => {
    //   console.log('/' + to.params.path, to, 'redirect')
    //   return { path: '/' + to.params.path, query: to.query }
    // }
  },
  {
    path: '/test',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        component: () => import('@/views/Test/index'),
        name: 'Test',
        meta: { title: '测试', icon: 'dashboard', affix: false }
      }
    ]
  },
  { path: '/login', component: () => import('@/views/Login'), hidden: true },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    hidden: true,
    children: [
      {
        path: '/index',
        component: () => import('@/views/Dashboard/index'),
        name: 'Dashboard',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },

  {
    path: '/404',
    component: () => import('@/views/Error/404'),
    hidden: true
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    return savedPosition || { top: 0 }
  }
})

// console.log(constantRoutes)

export default router
